package com.sdx.console.activity.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.activity.entity.ActivityActionPoint;
import com.sdx.console.activity.service.ActivityActionPointService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 活动积分 接口
 */
@RequestMapping("/activity/activityActionPoint")
@Controller
public class ActivityActionPointController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(ActivityActionPointController.class);
    private static final String MODEL = "/activity/activityActionPoint";


    @Resource
	private ActivityActionPointService activityActionPointService;

	/**
	 * 初始化页面需要的列表数据，
	 *
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request,Integer actId) {
        view.addObject("actId",actId);
	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("activity.activityActionPoint.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request,Integer actId) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request,actId);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("activity.activityActionPoint.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer actCode,Integer actId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        ActivityActionPoint activityActionPoint = activityActionPointService.selectPoint(actId,actCode);
        view.addObject("activityActionPoint", activityActionPoint);
		setPageParam(view,request,actId);
		try {
			view.addObject("activityActionPointJson", JacksonUtils.toJsonString(activityActionPoint,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("activity.activityActionPoint.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request,Integer actId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        view.addObject("activityActionPoint", new ActivityActionPoint());
		setPageParam(view,request,actId);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("activity.activityActionPoint.query")
    @ResponseBody
    public JsonResult<PageQuery> list(ActivityActionPoint condtion)
    {
        PageQuery page = condtion.getPageQuery();
        activityActionPointService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,ActivityActionPoint activityActionPoint,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
            ActivityActionPoint point = activityActionPointService.selectPoint(activityActionPoint.getActId(),activityActionPoint.getActCode());
            if(point!=null){
                result = JsonResult.failMessage("数据已存在");
            }else{
                activityActionPoint.setCreatedTime(new Date());
                activityActionPointService.save(activityActionPoint);
            }

		} else {
            activityActionPoint.setCreatedTime(new Date());
			activityActionPointService.updateTemplate(activityActionPoint);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("activity.activityActionPoint.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)ActivityActionPoint activityActionPoint,HttpServletRequest request
		)
    {

        return save(true,activityActionPoint,request
		);
    }

    @PostMapping("/update.json")
    @Function("activity.activityActionPoint.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  ActivityActionPoint activityActionPoint,HttpServletRequest request
		) {
		return save(false,activityActionPoint,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("activity.activityActionPoint.query")
    @ResponseBody
    public JsonResult<ActivityActionPoint>queryInfo(Integer actCode) {
        ActivityActionPoint activityActionPoint = activityActionPointService.queryById( actCode);
        return  JsonResult.success(activityActionPoint);
    }

    @PostMapping("/delete.json")
    @Function("activity.activityActionPoint.del")
    @ResponseBody
    public JsonResult delete(String ids,Integer actId) {
		List<String> idList = Utils.strToList(ids,",");
        activityActionPointService.batchDelActivityActionPoint(idList,actId);
        return JsonResult.success();
    }
    

}
